package com.pacy.pacybaseproject.viewmodels;

import android.app.Application;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.baidu.mapapi.search.route.BikingRoutePlanOption;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteLine;
import com.baidu.mapapi.search.route.MassTransitRoutePlanOption;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteLine;
import com.baidu.mapapi.search.route.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.blankj.utilcode.util.ToastUtils;
import com.pacy.pacybaseproject.adapters.RoutingBusAdapter;
import com.pacy.pacybaseproject.adapters.RoutingTravelAdapter;
import com.pacy.pacybaseproject.utils.Constants;
import com.pacy.pacybaseproject.utils.overlay.BikingRouteOverlay;
import com.pacy.pacybaseproject.utils.overlay.DrivingRouteOverlay;
import com.pacy.pacybaseproject.utils.overlay.MassTransitRouteOverlay;
import com.pacy.pacybaseproject.utils.overlay.TransitRouteOverlay;
import com.pacy.pacybaseproject.utils.overlay.WalkingRouteOverlay;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import java.util.logging.Handler;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.KLog;

/**
 * @author PACY
 * @date 2024/2/29
 * description:
 */
public class RoutingSearchViewModel extends BaseViewModel {
    public SingleLiveEvent<Boolean> showMap=new SingleLiveEvent<>(false);
    public String cityName="北京";
    public SingleLiveEvent<String> startNode=new SingleLiveEvent<>("北京北站");
    public SingleLiveEvent<String> endNode=new SingleLiveEvent<>("北京南站");
    public BaiduMap mBaiduMap;
    //路线检索
    public RoutePlanSearch mSearch;
    //监听
    public OnGetRoutePlanResultListener listener;


    //推荐公交路线adapter
    public RoutingTravelAdapter mAdapter;

    private Timer timer=new Timer();
    public RoutingSearchViewModel(@NonNull Application application) {
        super(application);
    }

    @Override
    protected void init() {
        super.init();
        initAdapter();
        initRoutingSearch();
        routingSearch(0);
    }

    public void initAdapter(){
        mAdapter=new RoutingTravelAdapter();
    }

    /**
     * 初始化路径规划
     */
    public void initRoutingSearch(){
        mSearch = RoutePlanSearch.newInstance();
        listener=new OnGetRoutePlanResultListener() {
            @Override
            public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {
                //创建WalkingRouteOverlay实例
                WalkingRouteOverlay overlay = new WalkingRouteOverlay(mBaiduMap);
                if(walkingRouteResult.getRouteLines()!=null){
                    if (walkingRouteResult.getRouteLines().size() > 0) {
                        //获取路径规划数据,(以返回的第一条路线为例）
                        //为DrivingRouteOverlay实例设置数据
                        overlay.setData(walkingRouteResult.getRouteLines().get(0));
                        //在地图上绘制DrivingRouteOverlay
                        overlay.addToMap();
                        overlay.zoomToSpan();
                        showContent();
                    }else{
                        showEmpty();
                    }
                }else{
                    ToastUtils.showShort("暂未查询到步行路线");
                    showEmpty();
                }
            }

            //火车路线规划回调
            @Override
            public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {
            }

            //跨城公交线路
            @Override
            public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {
//创建TransitRouteOverlay实例
//                MassTransitRouteOverlay overlay = new MassTransitRouteOverlay(mBaiduMap);
                //获取路径规划数据,(以返回的第一条数据为例)
                if(massTransitRouteResult.getRouteLines()!=null){
                    mAdapter.setNewInstance(massTransitRouteResult.getRouteLines());
                    showContent();
                }else{
                    ToastUtils.showShort("未查询到路线");
                    showEmpty();
                }
            }

            //驾车
            @Override
            public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
                //创建DrivingRouteOverlay实例
                DrivingRouteOverlay overlay = new DrivingRouteOverlay(mBaiduMap);
                if(drivingRouteResult.getRouteLines()!=null){
                    if (drivingRouteResult.getRouteLines().size() > 0) {
                        showContent();
                        //获取路径规划数据,(以返回的第一条路线为例）
                        //为DrivingRouteOverlay实例设置数据
                        overlay.setData(drivingRouteResult.getRouteLines().get(0));
                        //在地图上绘制DrivingRouteOverlay
                        overlay.addToMap();
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                overlay.zoomToSpan();
                            }
                        },300);
                    }else{
                        showEmpty();
                    }
                }else{
                    ToastUtils.showShort("暂未查询到驾车路线");
                    showEmpty();
                }

            }

            @Override
            public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

            }
            //骑行路线规划
            @Override
            public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {
                BikingRouteOverlay overlay = new BikingRouteOverlay(mBaiduMap);
                if(bikingRouteResult.getRouteLines()!=null){
                    if (bikingRouteResult.getRouteLines().size() > 0) {
                        showContent();
                        //获取路径规划数据,(以返回的第一条路线为例）
                        //为DrivingRouteOverlay实例设置数据
                        overlay.setData(bikingRouteResult.getRouteLines().get(0));
                        //在地图上绘制DrivingRouteOverlay
                        overlay.addToMap();
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                overlay.zoomToSpan();
                            }
                        },300);
                    }else{
                        showEmpty();
                    }
                }else{
                    ToastUtils.showShort("暂未查询到骑行路线");
                    showEmpty();
                }
            }
        };
        mSearch.setOnGetRoutePlanResultListener(listener);
    }

    /**
     * 公交路径规划
     * @param type 类型 0 1 公交 2 驾车 3 骑行
     */
    public void routingSearch(int type){
//        PlanNode stNode = PlanNode.withCityNameAndPlaceName("北京", "北京北站");
//        PlanNode enNode = PlanNode.withCityNameAndPlaceName("北京", "北京南站");
        PlanNode stNode = PlanNode.withLocation(new LatLng(39.947941,116.360948));
        PlanNode enNode = PlanNode.withLocation(new LatLng(39.87128,116.385488));
        switch (type){
            case 0:
            case 1:
                mSearch.masstransitSearch((new MassTransitRoutePlanOption())
                        .from(stNode)
                        .to(enNode));
                break;
            case 2:
                mSearch.drivingSearch((new DrivingRoutePlanOption())
                        .from(stNode)
                        .to(enNode));
                break;
            case 3:
                mSearch.bikingSearch((new BikingRoutePlanOption())
                        .from(stNode)
                        .to(enNode)
                        // ridingType  0 普通骑行，1 电动车骑行
                        // 默认普通骑行
                        .ridingType(0));
                break;
        }

        showLoading();
    }






    @Override
    public void onDestroy() {
        super.onDestroy();
        mSearch.destroy();
    }
}
